package com.xunji.standard.util;

import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;

public class FileUtils {

    private final static Logger logger = LoggerFactory.getLogger(FileUtils.class);

    public static boolean upload(MultipartFile file, String path) {
        boolean flag = true;
        FileOutputStream out = null;
        try {
            File targetFile = new File(path);
            if (!targetFile.exists()) {
                targetFile.getParentFile().mkdir();
                targetFile.createNewFile();
            }
            out = new FileOutputStream(targetFile, false);
            IOUtils.copy(file.getInputStream(), out);
            logger.info("文件上传成功，文件保存路径为:{}", targetFile.getAbsolutePath());
        } catch (Exception e) {
            flag = false;
            logger.error("文件上传失败");
        }finally {
            if(null != out){
                try{
                    out.close();
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }
        return flag;

    }

    // 写文件
    public static void writeFile(String content, String filePath) {
        FileOutputStream fos = null;
        BufferedWriter bw = null;
        try {

            File fout = new File(filePath);
            if (fout.exists()) {
                fout.delete();
            }
            fout.createNewFile();
            fos = new FileOutputStream(fout);
            bw = new BufferedWriter(new OutputStreamWriter(fos));
            bw.write(content);
            bw.flush();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                if(null != bw){
                    bw.close();
                }
                if(null != fos){
                    fos.close();
                }
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
    }

    // 读文件
    public static String readFile(String filePath) {
        File fout = new File(filePath);
        if (!fout.exists()) {
            return null;
        }
        FileReader fr = null;
        BufferedReader bf = null;
        String str = "";
        try {
            fr = new FileReader(filePath);
            bf = new BufferedReader(fr);
            str = bf.readLine();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if(null != bf){
                    bf.close();
                }
                if(null != fr){
                    fr.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return str;

    }

    public static File multipartFileToFile(MultipartFile file){

        File toFile = null;
        if (file.equals("") || file.getSize() <= 0) {
        } else {
            InputStream ins = null;
            try{
                ins = file.getInputStream();
                toFile = new File(file.getOriginalFilename());
                inputStreamToFile(ins, toFile);
                ins.close();
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return toFile;
    }

    public static void inputStreamToFile(InputStream ins, File file) {
        try {
            OutputStream os = null;
            try{
                if(null != file){
                    os = new FileOutputStream(file);
                }
                int bytesRead = 0;
                byte[] buffer = new byte[8192];
                while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
                    if(null != os){
                        os.write(buffer, 0, bytesRead);
                    }
                }
            }catch (Exception e){
                e.printStackTrace();
            }finally {
                if(null != os){
                    os.close();
                }
                if(null != ins){
                    ins.close();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
